#include<stdio.h>
#define FILEINP "LASER.INP"
#define FILEOUT "LASER.OUT"
int width,height,c,h;
int visited[10002]={0};
void input_init(){
    FILE *fp=fopen(FILEINP,"r");
    fscanf(fp,"%d%d%d%d",&height,&width,&c,&h);
    if(h==3)c=height-c;
    visited[c]=1;
}
int getResult(){
    long flag=0,result=0;
    do{
        if(((c+width)%height)==0)return result+width;
        if(((c+2*width)%height)==0)return result+2*width;
        c+=2*width;
        result+=2*width;
        flag=(c/height+flag)&1;
        c%=height;
        if(visited[flag==0?c:height-c]==0)visited[flag==0?c:height-c]=1;
        else return -1;
    }while(1);
}
int main(){
    input_init();
    FILE *fp=fopen(FILEOUT,"w"); 
    fprintf(fp,"%ld",getResult());
    fclose(fp);
    return 0;
}
